/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sunspotworld.protocols;

import com.sun.spot.peripheral.radio.IProtocolManager;
import com.sun.spot.peripheral.radio.IRadiogramProtocolManager;
import com.sun.spot.peripheral.radio.LowPanHeaderInfo;
import com.sun.spot.peripheral.radio.RadioPacket;
import org.sunspotworld.sensortasks.routingtasks.BidTask;
import org.sunspotworld.market.MarketAgent;
import org.sunspotworld.market.Reservation;
import org.sunspotworld.sensortasks.TimeKeeper;

/**
 *
 * @author me
 */
public class AwardProtocol implements IProtocolManager {
    private static boolean protocolEnabled = false;
    public static final byte PROTOCOL_NUM = MyProtocols.AWARD;
    private static final int PAYLOAD_SIZE = RadioPacket.MIN_PAYLOAD_LENGTH - IRadiogramProtocolManager.DATA_OFFSET;
    
    public static void setEnabled(boolean enabled) {
        protocolEnabled = enabled;
    }
    
    public void processIncomingData(byte[] arg0, LowPanHeaderInfo arg1) {
        if (!protocolEnabled) {
            return;
        }
        BidTask task = ((BidTask)TimeKeeper.getInstance().currentTask());
        task.setAddress(arg1.sourceAddress);
        MarketAgent.getInstance().addReservation(new Reservation(task, PAYLOAD_SIZE));
    }
}
